package com.atguigu.gulimail.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimail.member.vo.MemberLoginVo;
import com.atguigu.gulimail.member.vo.MemberRegistVo;
import com.atguigu.gulimail.member.vo.SocialUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.member.entity.MemberEntity;
import com.atguigu.gulimail.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author xujiale
 * @email 2519970360@qq.com
 * @date 2023-10-25 08:42:25
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    /**
     *  社交登录功能
     */
    @PostMapping("/oauth2/login")
    public R OAuth2Login(@RequestBody SocialUser socialUser){

        MemberEntity member=memberService.login(socialUser);

        if(member!=null){
            MemberResponseVo vo = new MemberResponseVo();
            BeanUtils.copyProperties(member,vo);

            return R.ok().put("data",vo);
        }else {
            return R.error();
        }

    }

    @PostMapping("/username")
    public R getByUserName(@RequestBody String userName){
        MemberEntity member = memberService.getByUserName(userName);
        if(member!=null){
            MemberResponseVo vo = new MemberResponseVo();
            BeanUtils.copyProperties(member,vo);

            return R.ok().put("data",vo);
        }else {
            return R.error();
        }
    }

    /**
     * 登录功能
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo loginVo){

        return memberService.login(loginVo);
    }

    /**
     *  注册功能
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo registVo){
        return memberService.saveRegist(registVo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
